package com.cws.controller.sys;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.cws.dto.PageReq;
import com.cws.enmu.RspCode;
import com.cws.frame.interceptor.Post;
import com.cws.model.SysUser;
import com.cws.service.UserService;
import com.cws.sys.Constant;
import com.cws.utils.WSUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class UserController extends Controller {

	@Before(Post.class)
	public void getByPage() {
		String json = getAttr(Constant.REQ_BODY);
		PageReq req = JSON.parseObject(json, PageReq.class);

		System.out.println(req.toString());
		System.out.println(JSON.toJSON(req));
		Page<SysUser> rsp = UserService.getInstance().getByPage(req);
		renderJson(rsp);

	}

	public void getById() {
		String id = getPara();
		SysUser u = SysUser.dao.findById(id);
		renderJson(u);
	};

	@Before(Post.class)
	public void add() {
		String json = getAttr(Constant.REQ_BODY);

		Map<String, Object> attrMap = JSON.parseObject(json, new TypeReference<Map<String, Object>>() {});
		SysUser u = new SysUser().setAttrs(attrMap);
		
		u.save();
		renderJson(RspCode.SUCCESS.toJson());
	}

	public void getAll() {
		renderJson(WSUtil.createRsp(RspCode.SUCCESS, UserService.getInstance().getAllUser()));
	}
}
